package com.hu.jie.day01;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.gc.flashview.FlashView;
import com.google.gson.Gson;
import com.hu.jie.day01.adapter.MyAdapter;
import com.hu.jie.day01.bean.LiWuShuoBean;
import com.hu.jie.day01.contancs.UrlString;
import com.hu.jie.day01.utils.OKHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.coll_toolbar)
    CollapsingToolbarLayout collToolbar;
    @InjectView(R.id.tv1)
    TextView tv1;
    @InjectView(R.id.tv2)
    TextView tv2;
    @InjectView(R.id.tv3)
    TextView tv3;
    @InjectView(R.id.tl_top)
    TabLayout tlTop;
    @InjectView(R.id.app1)
    AppBarLayout app1;
    @InjectView(R.id.vp_content)
    ViewPager vpContent;
    private List<String> mDatas;
    private List<String> mTabs;
    private ViewPager vp;
    private FlashView fv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        fv = (FlashView) findViewById(R.id.fv);
        mDatas = new ArrayList<>();
        mTabs = new ArrayList<>();
        vpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
        setSupportActionBar(toolbar);
        tlTop.setupWithViewPager(vpContent);

        OKHttpUtils.newInstance(this).getAsyncMainData(UrlString.HOME_URL, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(final String result) {
                final LiWuShuoBean bean = new Gson().fromJson(result, LiWuShuoBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv1.setText(bean.getData().getName());
                        tv2.setText(bean.getData().getPrice());
                        tv3.setText(bean.getData().getDescription());
                        fv.setImageUris(bean.getData().getImage_urls());
                        mTabs.add("图文评论");
                        mTabs.add("评论(" + bean.getData().getComments_count() + ")");
                        init();
                    }
                });
            }
        });
    }

    private void init() {
        for (int i = 0; i < tlTop.getTabCount(); i++) {
            TabLayout.Tab tab = tlTop.getTabAt(i);
            tab.setText(mTabs.get(i));
        }
    }


}
